//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "CommoditySettings.h"
using namespace Cephei::QL::Experimental::Commodities;
#include <gen/QL/Currency.h>
#include <gen/QL/Experimental/Commodities/UnitOfMeasure.h>
using namespace Cephei::QL;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Commodities::CCommoditySettings::CCommoditySettings (boost::shared_ptr<QuantLib::CommoditySettings>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCommoditySettings = NULL;
#endif
	_ppCommoditySettings = &childNative;
    
}
Cephei::QL::Experimental::Commodities::CCommoditySettings::CCommoditySettings (QuantLib::CommoditySettings& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phCommoditySettings = NULL;
#endif
	_ppCommoditySettings = new boost::shared_ptr<QuantLib::CommoditySettings> (&childNative);
    
    _CommoditySettingsOwner = owner;
    
}

Cephei::QL::Experimental::Commodities::CCommoditySettings::CCommoditySettings (CCommoditySettings^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCommoditySettings = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppCommoditySettings = new boost::shared_ptr<QuantLib::CommoditySettings> (copy->GetShared());
        
    }
}
Cephei::QL::Experimental::Commodities::CCommoditySettings::CCommoditySettings (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCommoditySettings = NULL;
#endif
	if (!t->IsSubclassOf(CCommoditySettings::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Commodities::CCommoditySettings::CCommoditySettings (QuantLib::Handle<QuantLib::CommoditySettings>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phCommoditySettings = &childNative;
	_ppCommoditySettings = &static_cast<boost::shared_ptr<QuantLib::CommoditySettings>>(childNative.currentLink());
    
    _CommoditySettingsOwner = owner;
}
Cephei::QL::Experimental::Commodities::CCommoditySettings::CCommoditySettings (QuantLib::Handle<QuantLib::CommoditySettings> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phCommoditySettings = &childNative;
	_ppCommoditySettings = &static_cast<boost::shared_ptr<QuantLib::CommoditySettings>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Commodities::CCommoditySettings::CCommoditySettings (QuantLib::CommoditySettings childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCommoditySettings = NULL;
#endif
	_ppCommoditySettings = new boost::shared_ptr<QuantLib::CommoditySettings> (new QuantLib::CommoditySettings (childNative));
    
}
#endif

Cephei::QL::Experimental::Commodities::CCommoditySettings::~CCommoditySettings ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppCommoditySettings != NULL)
    {
	    delete _ppCommoditySettings;
        _ppCommoditySettings = NULL;
    }
}
Cephei::QL::Experimental::Commodities::CCommoditySettings::!CCommoditySettings ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppCommoditySettings != NULL)
    {
	    delete _ppCommoditySettings;
    }
}
QuantLib::CommoditySettings& Cephei::QL::Experimental::Commodities::CCommoditySettings::GetReference ()
{
    if (_ppCommoditySettings == NULL) throw gcnew NativeNullException ();
	return **_ppCommoditySettings;
}
boost::shared_ptr<QuantLib::CommoditySettings>& Cephei::QL::Experimental::Commodities::CCommoditySettings::GetShared ()
{
    if (_ppCommoditySettings == NULL) throw gcnew NativeNullException ();
	return *_ppCommoditySettings;
}
QuantLib::CommoditySettings* Cephei::QL::Experimental::Commodities::CCommoditySettings::GetPointer ()
{
    if (_ppCommoditySettings == NULL) throw gcnew NativeNullException ();
	return &**_ppCommoditySettings;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::CommoditySettings>& Cephei::QL::Experimental::Commodities::CCommoditySettings::GetHandle ()
{
	if (_phCommoditySettings == NULL)
	{
		_phCommoditySettings = new Handle<QuantLib::CommoditySettings> (*_ppCommoditySettings);
	}
	return *_phCommoditySettings;
}
#endif
bool Cephei::QL::Experimental::Commodities::CCommoditySettings::HasNative () 
{
	return (_ppCommoditySettings != NULL);
}

Cephei::QL::ICurrency^ Cephei::QL::Experimental::Commodities::CCommoditySettings::Currency::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::Currency& _rv = (QuantLib::Currency&)(*_ppCommoditySettings)->currency ( );   
        Cephei::QL::CCurrency^ _nrv = gcnew Cephei::QL::CCurrency (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Commodities::IUnitOfMeasure^ Cephei::QL::Experimental::Commodities::CCommoditySettings::UnitOfMeasure::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::UnitOfMeasure& _rv = (QuantLib::UnitOfMeasure&)(*_ppCommoditySettings)->unitOfMeasure ( );   
        Cephei::QL::Experimental::Commodities::CUnitOfMeasure^ _nrv = gcnew Cephei::QL::Experimental::Commodities::CUnitOfMeasure (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

